package com.example.city_home_app.Activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.CardView;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.EditText;

import com.example.city_home_app.Bean.Down_Captcha;
import com.example.city_home_app.Bean.Down_Universe;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.UIUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgotActivity extends BaseActivity {
    @BindView(R.id.et_forgotemail)
    EditText etForgotemail;
    @BindView(R.id.btn_forgot_getcheckcode)
    Button btnForgotGetcheckcode;
    @BindView(R.id.et_forgotcheckcode)
    EditText etForgotcheckcode;
    @BindView(R.id.et_forgotpassword)
    EditText etForgotpassword;
    @BindView(R.id.bt_forgot_go)
    Button btForgotGo;
    @BindView(R.id.cv_forgot)
    CardView cvForgot;
    @BindView(R.id.forgot_fab)
    FloatingActionButton forgotFab;

    private int codeid;
    private String email,code;
    private int type;

    private RetrofitListener<Down_Captcha> captchaRetrofitListener = new RetrofitListener<Down_Captcha>() {
        @Override
        public void onResponse(Down_Captcha listener) {
            codeid = listener.getId();
            code = listener.getCode();
            UIUtils.showToast(ForgotActivity.this, "已发送邮件，请前往邮箱查收验证码", UIUtils.LENGTH_SHORT);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private RetrofitListener<Down_Universe> forgotretrofitListener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if(listener.getMessage().equals("success")){
                UIUtils.showToast(ForgotActivity.this, "修改密码成功", UIUtils.LENGTH_SHORT);
                onBackPressed();
            }else{
                UIUtils.showToast(ForgotActivity.this, listener.getMessage(), UIUtils.LENGTH_SHORT);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_forgot;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this);
        ShowEnterAnimation();
    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        type = intent.getIntExtra("type",0);
    }

    private void ShowEnterAnimation() {
        Transition transition = TransitionInflater.from(this).inflateTransition(R.transition.fabtransition);
        getWindow().setSharedElementEnterTransition(transition);

        transition.addListener(new Transition.TransitionListener() {
            @Override
            public void onTransitionStart(Transition transition) {
                cvForgot.setVisibility(View.GONE);
            }

            @Override
            public void onTransitionEnd(Transition transition) {
                transition.removeListener(this);
                animateRevealShow();
            }

            @Override
            public void onTransitionCancel(Transition transition) {

            }

            @Override
            public void onTransitionPause(Transition transition) {

            }

            @Override
            public void onTransitionResume(Transition transition) {

            }


        });
    }

    public void animateRevealShow() {
        Animator mAnimator = ViewAnimationUtils.createCircularReveal(cvForgot, cvForgot.getWidth() / 2, 0, forgotFab.getWidth() / 2, forgotFab.getHeight());
        mAnimator.setDuration(500);
        mAnimator.setInterpolator(new AccelerateInterpolator());
        mAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }

            @Override
            public void onAnimationStart(Animator animation) {
                cvForgot.setVisibility(View.VISIBLE);
                super.onAnimationStart(animation);
            }
        });
        mAnimator.start();
    }

    public void animateRevealClose() {
        Animator mAnimator = ViewAnimationUtils.createCircularReveal(cvForgot, cvForgot.getWidth() / 2, 0, cvForgot.getHeight(), forgotFab.getWidth() / 2);
        mAnimator.setDuration(500);
        mAnimator.setInterpolator(new AccelerateInterpolator());
        mAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                cvForgot.setVisibility(View.INVISIBLE);
                super.onAnimationEnd(animation);
                forgotFab.setImageResource(R.drawable.plus);
                ForgotActivity.super.onBackPressed();
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }
        });
        mAnimator.start();
    }

    @Override
    public void onBackPressed() {
        animateRevealClose();
    }


    @OnClick({R.id.btn_forgot_getcheckcode, R.id.bt_forgot_go, R.id.forgot_fab})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_forgot_getcheckcode:
                if (UIUtils.isemail(etForgotemail.getText().toString())) {
                    email = etForgotemail.getText().toString();
                    ApiModel model = new ApiModel(1);
                    model.getcheckcode(email, captchaRetrofitListener);
                } else {
                    UIUtils.showToast(this, "邮箱格式错误，请重新输入", UIUtils.LENGTH_SHORT);
                }
                break;
            case R.id.bt_forgot_go:
                forgot();
                break;
            case R.id.forgot_fab:
                if(type==1){
                    Intent intent = new Intent(this,LoginActivity.class);
                    startActivity(intent);
                    finish();
                }else {
                    onBackPressed();
                }
                break;
        }
    }

    private void forgot(){
        if(!etForgotcheckcode.getText().toString().isEmpty()&&!etForgotpassword.getText().toString().isEmpty()&&!etForgotemail.getText().toString().isEmpty()){
            if(UIUtils.isusername(etForgotpassword.getText().toString())){
                ApiModel model = new ApiModel();
                model.forgotpassword(etForgotemail.getText().toString(),etForgotcheckcode.getText().toString(),codeid,etForgotpassword.getText().toString(),forgotretrofitListener);
            }else{
                UIUtils.showToast(this, "密码长度限制为6-16位,且不允许特殊字符", UIUtils.LENGTH_SHORT);
            }
        }else{
            UIUtils.showToast(this, "请输入完整信息", UIUtils.LENGTH_SHORT);
        }
    }
}
